package com.fingard.dsp.bankset;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.fingard.db.JDBCHelper;

public class BankPayInfoHis {

    public int count = 1;
    public int activeDate = 100101;
    public boolean isSuccess = false;
    public Connection conn;

    public BankPayInfoHis(Connection p_conn) {
        conn = p_conn;
    }

    private static String queryHis = "select active_date activedate,crt_tbl_count count from tbl_dspcl_pay_info_crt_his where active_date=?";
    private static String queryLastHis = "select active_date activedate,crt_tbl_count count from tbl_dspcl_pay_info_crt_his where active_date<=? order by active_date desc";
    private static String insertHis = "insert into tbl_dspcl_pay_info_crt_his (urid,crt_tbl_count,active_date) values (?,?,?)";

    public void queryHisInfo(int p_activeDate) throws Exception {
        Map<String, Object> hisInfoMap = JDBCHelper.execQuerySingle(this.conn, queryHis, new Object[]{p_activeDate});
        if (hisInfoMap != null && hisInfoMap.size() > 0) {
            this.count = Integer.parseInt(String.valueOf(hisInfoMap.get("count")));
            this.activeDate = Integer.parseInt(String.valueOf(hisInfoMap.get("activedate")));
            this.isSuccess = true;
        }
    }

    public void queryLastHisInfo(String p_bgnDate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date tmpDate = sdf.parse(p_bgnDate);
        sdf = new SimpleDateFormat("yyMMdd");
        String strBgnDate = sdf.format(tmpDate);
        Map<String, Object> hisInfoMap = JDBCHelper.execQuerySingle(this.conn, queryLastHis, new Object[]{Integer.parseInt(strBgnDate)});
        if (hisInfoMap != null && hisInfoMap.size() > 0) {
            this.count = Integer.parseInt(String.valueOf(hisInfoMap.get("count")));
            this.activeDate = Integer.parseInt(String.valueOf(hisInfoMap.get("activedate")));
            this.isSuccess = true;
        } else {
            throw new Exception("支付信息表有误，请排查！");
        }
    }

    public void insertHisInfo() throws Exception {
        this.isSuccess = JDBCHelper.update(this.conn, insertHis, new Object[]{UUID.randomUUID().toString().replace("-", ""), this.count, this.activeDate}) > 0;
    }

}
